package com.android.ailaoshan.collection;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.android.ailaoshan.common.ResourceInfo;

import org.litepal.LitePal;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManagerFile {

    private final String PIC_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Picture" + File.separator;
    private final String SOUND_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Sound" + File.separator;

    /**
     * 从数据库中查找已存在的记录
     */
    public void searchAllFile(String fileType, Handler myHandler){
        new Thread(
            () -> {
                List<ResourceInfo> resourceInfoList = LitePal
                        .where("fileType=?",fileType).order("id")
                        .find(ResourceInfo.class);

                // 存储ArrayList对象
                HashMap<Long,ArrayList<ResourceInfo>> resourceMap = null;

                // 需要显示的信息
                ArrayList<CustomFileInfo> customFileInfos = new ArrayList<>();

                if(resourceInfoList.size() > 0){
                    resourceMap = new HashMap<>();

                    // 把属于同一批次的文件名归类
                    for(ResourceInfo resourceInfo : resourceInfoList){
                        // 文件批次
                        long fileNo = resourceInfo.getFileNo();

                        // 从Map里取出当前文件批次的记录
                        ArrayList<ResourceInfo> myResource = resourceMap.get(fileNo);

                        // 如果存在记录，则再添加一条记录
                        if(myResource != null){
                            myResource.add(resourceInfo);
                        }else{
                            myResource = new ArrayList<>();
                            myResource.add(resourceInfo);
                        }
                        resourceMap.put(fileNo,myResource);
                    }

                    // 提取出需要展示的信息
                    for( Map.Entry<Long,ArrayList<ResourceInfo>> entry : resourceMap.entrySet() ){
                        CustomFileInfo customFileInfo = new CustomFileInfo();

                        // 一个批次文件信息
                        ArrayList<ResourceInfo> useResources = entry.getValue();

                        // 文件批次
                        customFileInfo.setFileNo(entry.getKey());

                        // 遍历同一个批次文件名
                        ArrayList<String> moreFileName = new ArrayList<>();
                        // 找出文件描述信息
                        String describe = "";
                        // 是否上传
                        boolean upload = false;
                        for(ResourceInfo useResource : useResources){
                            moreFileName.add(useResource.getName());
                            if( useResource.getDescribe() != null){
                                if( useResource.getDescribe().length() > 0 ){
                                    describe = useResource.getDescribe();
                                }
                            }
                            if(useResource.getUpload() == 1){
                                upload = true;
                            }
                        }

                        customFileInfo.setMoreFileName(moreFileName);
                        customFileInfo.setDescribe(describe);
                        customFileInfo.setUpload(upload);

                        customFileInfos.add(customFileInfo);
                    }
                }

                // 回调
                Message msg = myHandler.obtainMessage();
                if(resourceMap == null){
                    msg.what = 606;
                }else{
                    msg.what = 605;
                }
                msg.obj = customFileInfos;

                myHandler.sendMessage(msg);
            }
        ).start();
    }


    /**
     * 删除数据库记录和本地文件
     */
    public void deleteFile(long fileNo,ArrayList<String> list, int fileType){
        new Thread(
            () -> {
                // 删除数据库记录
                LitePal.deleteAll(ResourceInfo.class,"fileNo = ?",""+fileNo);

                String finalPath;
                if(fileType == 1){
                    finalPath = PIC_PATH;
                }else{
                    finalPath = SOUND_PATH;
                }

                // 删除本地文件
                for(String name : list){
                    String filePath = finalPath + name;
                    File file = new File(filePath);
                    if(file.exists()){
                        file.delete();
                    }
                }
            }
        ).start();
    }
}
